import json
import os


def modify_json_category_in_dir(directory, old_category, new_category):
    for filename in os.listdir(directory):
        if filename.endswith(".json"):
            file_path = os.path.join(directory, filename)
            modify_json_category(file_path, old_category, new_category)


def modify_json_category(file_path, old_category, new_category):
    with open(file_path, 'r', encoding='utf-8') as f:
        data = json.load(f)

    if "objects" in data:
        for obj in data["objects"]:
            if obj.get("category") == old_category:
                obj["category"] = new_category

    with open(file_path, 'w', encoding='utf-8') as f:
        json.dump(data, f, indent=4, ensure_ascii=False)


# 示例调用
modify_json_category_in_dir(r"E:\2025-03-05_16_17_15-", "__background__", "black")